//
//  TabbarVC.swift
//  WebViewGoback
//
//  Created by 郑东喜 on 2017/2/24.
//  Copyright © 2017年 郑东喜. All rights reserved.
//

import UIKit

class TabbarVC: BaseTabbarVC {
    
    let prefix : String = NSHomeDirectory() + "/Documents/bj/zip/"
    var tabbarIcon : NSDictionary? {
        didSet {

            print("\((#file as NSString).lastPathComponent):(\(#line))\n")
            
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        
        
        let manager = FileManager.default
        let urlForDocument = manager.urls(for: .documentDirectory, in:.userDomainMask)
        let url = urlForDocument[0] as URL
        print(url)
        
        let contentsOfPath = try? manager.contentsOfDirectory(atPath: NSHomeDirectory() + "/Documents/bj/zip")
        print("contentsOfPath: \(contentsOfPath)")
        
        
        print("\((#file as NSString).lastPathComponent):(\(#line))\n",FileExist.fileIsExist(filePathStr: "/Documents/bj"))
        

        
        let vc1 = UIViewController()
        vc1.view.backgroundColor = UIColor.randomColor()
        
        let vc2 = UIViewController()
        vc2.view.backgroundColor = UIColor.randomColor()
        
        let vc3 = UIViewController()
        vc3.view.backgroundColor = UIColor.randomColor()
        
        let vc4 = UIViewController()
        vc4.view.backgroundColor = UIColor.randomColor()
        
        let vc5 = UIViewController()
        vc4.view.backgroundColor = UIColor.randomColor()
        
        let nav = UINavigationController.init(rootViewController: vc1)

        let nav2 = UINavigationController.init(rootViewController: vc2)
        
        let nav3 = UINavigationController.init(rootViewController: vc3)
        
        let nav4 = UINavigationController.init(rootViewController: vc4)
        
        let nav5 = UINavigationController.init(rootViewController: vc5)

        
        let fileManager = FileManager.default
        let filePath:String = NSHomeDirectory() + "/Documents/bj/zip"
        let exist = fileManager.fileExists(atPath: filePath)
        
        //1.是否本地已存在下载好的图标文件夹
        if exist == true {
        //2.有 执行什么操作
            
            TabbarIconModel.shared.getTabbarIcon { (dicc) in
                
                self.tabbarIcon = dicc
                
                TabbarIconModel.shared.one_nor = self.tabbarIcon?["id_1_normal"] as? String
                TabbarIconModel.shared.two_nor = self.tabbarIcon?["id_2_normal"] as? String
                TabbarIconModel.shared.three_nor = self.tabbarIcon?["id_3_normal"] as? String
                TabbarIconModel.shared.four_nor = self.tabbarIcon?["id_4_normal"] as? String
                TabbarIconModel.shared.five_nor = self.tabbarIcon?["id_5_normal"] as? String
                
                TabbarIconModel.shared.one_sele = self.tabbarIcon?["id_1_selected"] as? String
                TabbarIconModel.shared.two_sele = self.tabbarIcon?["id_2_selected"] as? String
                TabbarIconModel.shared.three_sele = self.tabbarIcon?["id_3_selected"] as? String
                TabbarIconModel.shared.four_sele = self.tabbarIcon?["id_4_selected"] as? String
                TabbarIconModel.shared.five_sele = self.tabbarIcon?["id_5_selected"] as? String
                
                TabbarIconModel.shared.tabbarBgImgName = self.tabbarIcon?["tarbarBgName"] as? String
                
                self.setupChildVC(nav, title: "vc1", imageName: self.prefix + TabbarIconModel.shared.one_nor!, selectImageName: self.prefix + TabbarIconModel.shared.one_sele!)
                self.setupChildVC(nav2, title: "vc2", imageName: self.prefix + TabbarIconModel.shared.two_nor!, selectImageName: self.prefix + TabbarIconModel.shared.two_sele!)
                
                if self.centerIsBig == true {
                    self.setupChildVC(nav3, title: "vc3", imageName: self.prefix + TabbarIconModel.shared.three_nor!, selectImageName: self.prefix + TabbarIconModel.shared.three_sele!)
                } else {
                    self.setupChildVC(nav3, title: "vc3", imageName: "messageA", selectImageName: "messageL")
                }
                
                self.setupChildVC(nav4, title: "vc4", imageName: self.prefix + TabbarIconModel.shared.four_nor!, selectImageName: self.prefix + TabbarIconModel.shared.four_sele!)
                
                self.setupChildVC(nav5, title: "vc5", imageName: self.prefix + TabbarIconModel.shared.five_nor!, selectImageName: self.prefix + TabbarIconModel.shared.five_sele!)
            }

        } else {
            //3.没有  执行什么操作
            
            self.setupChildVC(nav, title: "vc1", imageName: "homeA", selectImageName: "homeL")
            self.setupChildVC(nav2, title: "vc2", imageName: "reportA", selectImageName: "reportL")

            
            if self.centerIsBig == true {
                self.setupChildVC(nav3, title: "vc3", imageName: "baseAct", selectImageName: "baseAct")
            } else {
                self.setupChildVC(nav3, title: "vc3", imageName: "messageA", selectImageName: "messageL")
            }
            
            self.setupChildVC(nav4, title: "vc4", imageName: "messageA", selectImageName: "messageL")
            
            self.setupChildVC(nav5, title: "vc5", imageName: "messageA", selectImageName: "messageL")
        }
        
        
    }

    //添加子页面
    func setupChildVC(_ childVC: UIViewController,title: String,imageName: String,selectImageName: String) {
        
        childVC.title = title
        childVC.tabBarItem.image = UIImage.init(named: imageName)
        //        不在渲染图片
        childVC.tabBarItem.selectedImage = UIImage.init(named: selectImageName)?.withRenderingMode(.alwaysOriginal)
        
        self.addChildViewController(childVC)
    }
}

extension TabbarVC {
    func addIcon() -> Void {
        
    }
}

extension TabbarVC {
    
    //本地tabbar图标配置文件
    fileprivate func tabbarIconSEL() -> Bool {
        let fileManager = FileManager.default
        let filePath:String = NSHomeDirectory() + "/Documents/bj/tabbarIcon.plist"
        let exist = fileManager.fileExists(atPath: filePath)
        
        return exist
    }
}
